﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace GeneralDashboard.Controllers
{
    /// <summary>
    /// EChart.xaml 的交互逻辑
    /// </summary>
    public partial class DynamicSortBar : UserControl
    {
        public DynamicSortBar()
        {
            InitializeComponent();
            DataDic = new Dictionary<string, int>();
            webView.Source = new Uri(new FileInfo("DynamicBar.html").FullName);
            InitializeAsync();
        }

        public void AddSerial(string name)
        {
            DataDic.Add(name, 0);
            webView.CoreWebView2.ExecuteScriptAsync($"addSerial({name});");
        }

        public Dictionary<string, int> DataDic { get; set; }
        public SolidColorBrush SerialColor
        {
            get { return (SolidColorBrush)GetValue(SerialColorProperty); }
            set { SetValue(SerialColorProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SerialColor.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SerialColorProperty =
            DependencyProperty.Register("SerialColor", typeof(SolidColorBrush), typeof(DynamicSortBar), new PropertyMetadata(new SolidColorBrush(Colors.BlueViolet), async (s, e) =>
            {
                var c = s as DynamicSortBar;
                var value = e.NewValue as SolidColorBrush;
                var color = ColorTranslator.ToHtml(System.Drawing.Color.FromArgb(value.Color.A, value.Color.R, value.Color.G, value.Color.B));
                await c.webView.EnsureCoreWebView2Async();
                await c.webView.CoreWebView2.ExecuteScriptAsync($"setColor('{color}');");
            }));

        public async void InitializeAsync()
        {
            await webView.EnsureCoreWebView2Async();
            await webView.CoreWebView2.ExecuteScriptAsync(
                "window.addEventListener('dragover',function(e){e.preventDefault();},false);");
            await webView.CoreWebView2.ExecuteScriptAsync(
                "window.addEventListener('drop',function(e){" +
                "e.preventDefault();" +
                "console.log(e.dataTransfer);" +
                "console.log(e.dataTransfer.files[0])" +
                "}, false);");
            await webView.CoreWebView2.ExecuteScriptAsync(
                "window.addEventListener('contextmenu', window => {window.preventDefault();});");
            await Task.Run(() =>
            {
                while (true)
                {
                    Task.Delay(3000).Wait();
                    var keys = DataDic.Keys.ToList();
                    for (int i = 0; i < DataDic.Count; i++)
                    {
                        var r = new Random().NextDouble();
                        if (r > 0.9)
                        {
                            DataDic[keys[i]] += 2000;
                        }
                        else
                        {
                            DataDic[keys[i]] += 200;
                        }
                    }
                    keys.Reverse();
                    var sb = new StringBuilder("refreshData([");
                    for(int i = 0; i < DataDic.Count; i++)
                    {
                        sb.Append(DataDic[keys[i]]).Append(',');
                    }
                    sb.Remove(sb.Length - 1, 1);
                    sb.Append("]);");

                    webView.Dispatcher.Invoke(new Action(() => {
                        webView.CoreWebView2.ExecuteScriptAsync(
                        sb.ToString());
                    }));
                }
            });
        }
        int i = 0;
        private void testButton_Click(object sender, RoutedEventArgs e)
        {
            AddSerial((i++).ToString());
        }
    }
}
